<template>
  <base-layout
    :dialog-visible="dialogVisible"
    :dialog-close-confirm="false"
    dialog-title="采购详情"
    @dialog-close="dialogClose"
    :dialogWidth="dialogWidth">
    <template #searchPanel>
      <search-panel ref="searchPanel" v-slot="{ form }" addTitle="新增采购" @search="search" @insert="addHandle" showAddButton :addPer="authConfig.add" :selectPer="authConfig.query">
        <job-shop v-model="form.storeId" placeholder="所属门店" />
        <el-select v-model="form.status" clearable placeholder="采购状态">
          <el-option v-for="item in dict['采购状态']" :key="item.index" :value="item.value" :label="item.text"/>
        </el-select>
        <el-input v-model="form.code" clearable placeholder="采购编号" style="width: 200px"/>
      </search-panel>
    </template>

    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @row-dblclick="detailHandle"
        :width="160"
      >
        <template #action="{row}">
          <i
            v-if="row.status != '已入库'"
            class="el-icon-back pointer"
            title="退货"
            v-permission="authConfig.return"
            @click="returnHandle(row)"
          ></i>
          <i
            class="el-icon-edit pointer ml-20"
            title="编辑"
            v-permission="authConfig.edit"
            @click="editHandle(row, '编辑')"
          ></i>
          <i class="el-icon-delete pointer ml-20" title="删除" v-permission="authConfig.del" @click="handleDataDelete(row)"></i>
        </template>
      </data-table>
      <!--工具条-->

    </template>
    <template #dialog>
      <purchase-detail v-if="detailData" :detail-data="detailData" @refresh="setTableData"/>
    </template>
  </base-layout>
</template>

<script>
  import baseMixin from '@/mixins/base'
  import { getDict } from '@/utils/index'
  import PurchaseDetail from './components/purchaseRequestDetail'
  import { pageList, deleteById } from '@/api/pre-sale/boutiquePurchase/apply'
  // import { parseTimeToYMDHMS } from '@/utils'

  const dict = getDict(['采购状态'])

  // 采购申请页面
  export default {
    components: {
      PurchaseDetail
    },
    mixins: [
      baseMixin({
        findData: pageList,
        dataName: ['content', 'totalRecords'],
        deleteData: deleteById,
        idName: 'id',
        format(res) {
          return res.map(data => {
            // data.createTime = data.createTime ? parseTimeToYMDHMS(data.createTime) : ''
            return data
          })
        }
      })
    ],
    data() {
      this.authConfig = {
        query: 'purchaseRecord:page',
        add: 'purchaseRecord:save',
        edit: 'purchaseRecord:update',
        del: 'purchaseRecord:del',
        return: 'purchaseReturn:save'
      }

      return {
        nullValue: null,
        dialogVisible: false,
        dialogWidth: '',
        tableColumnAttributes: [
          { name: '采购门店', value: 'storeName', type: 'text' },
          { name: '采购编号', value: 'code', type: 'text' },
          { name: '采购类型', value: 'type', type: 'dict', dict: '采购类型' },
          { name: '采购精品数量', value: 'partsNum', type: 'text' },
          { name: '采购供应商', value: 'supplierName', type: 'text' },
          { name: '采购人员', value: 'userName', type: 'text' },
          { name: '负责人联系方式', value: 'phone', type: 'text' },
          { name: '采购总金额', value: 'amount', type: 'text' },
          { name: '采购备注', value: 'remark', type: 'text' },
          { name: '采购状态', value: 'status' },
          { name: '采购时间', value: 'createTime', type: 'text' }
        ],
        tableData: [],
        detailData: null
      }
    },
    methods: {
      addHandle() {
        this.$router.push({ name: 'boutiquePurchaseApplyAdd', params: { id: 0 }})
      },
      returnHandle(row) {
        this.$router.push({ name: 'boutiquePurchaseReturnAdd', params: { id: 0, returnType: '采购退货', purchaseCode: row.code }})
      },
      editHandle(row) {
        this.$router.push({ name: `boutiquePurchaseApplyEdit`, params: { id: row.id }})
      },
      detailHandle(row) {
        this.dialogWidth = '80%'
        this.detailData = row
        this.dialogVisible = true
      },
      search(form) {
        this.searchOptions = {
          storeId: form.storeId,
          status: form.status,
          code: form.code
        }
        this.currentPage = 1
        this.setTableData()
      }
    },
    created() {
      this.dict = dict
    },
    mounted() {

    }
  }
</script>

<style lang="scss" scoped>

</style>
